package com.wtx.fitness.common;

import java.io.Serializable;

/**
 * 统一API响应结果封装
 */
public class CommonResult<T> implements Serializable {
  private static final long serialVersionUID = 1L;

  /**
   * 状态码
   */
  private int code;

  /**
   * 消息
   */
  private String message;

  /**
   * 数据
   */
  private T data;

  /**
   * 私有构造函数，通过静态方法创建实例
   */
  private CommonResult() {
  }

  /**
   * 全参构造函数
   */
  private CommonResult(int code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * 返回成功结果
   */
  public static <T> CommonResult<T> success() {
    return new CommonResult<>(200, "操作成功", null);
  }

  /**
   * 返回成功结果
   */
  public static <T> CommonResult<T> success(T data) {
    return new CommonResult<>(200, "操作成功", data);
  }

  /**
   * 返回成功结果
   */
  public static <T> CommonResult<T> success(String message, T data) {
    return new CommonResult<>(200, message, data);
  }

  /**
   * 返回失败结果
   */
  public static <T> CommonResult<T> failed(String message) {
    return new CommonResult<>(500, message, null);
  }

  /**
   * 返回失败结果
   */
  public static <T> CommonResult<T> failed(int code, String message) {
    return new CommonResult<>(code, message, null);
  }

  /**
   * 参数验证失败返回结果
   */
  public static <T> CommonResult<T> validateFailed(String message) {
    return new CommonResult<>(400, message, null);
  }

  /**
   * 未登录返回结果
   */
  public static <T> CommonResult<T> unauthorized(String message) {
    return new CommonResult<>(401, message, null);
  }

  /**
   * 未授权返回结果
   */
  public static <T> CommonResult<T> forbidden(String message) {
    return new CommonResult<>(403, message, null);
  }

  /**
   * 判断是否成功
   */
  public boolean isSuccess() {
    return this.code == 200;
  }

  // Getters and Setters

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }
}